package reflect;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test2 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        File dir=new File(
                Test2.class.getResource(".").toURI());

        String packname=Test2.class.getPackage().getName();
        System.out.println(packname);

        File[] file=dir.listFiles(f->f.getName().endsWith(".class"));


       for (File f:file){
           String filename=f.getName();
           String classname=filename.substring(0,filename.indexOf("."));
           System.out.println(classname);
           Class cls=Class.forName(packname+"."+classname);
           Object obj=cls.newInstance();
           Method[] methods= cls.getDeclaredMethods();
           for(Method m:methods){
               String sname=m.getName();
               if (sname.contains("main")){
                   if(m.getParameterCount()==0
                   ){
                       m.setAccessible(true);
                       m.invoke(obj);
                       m.setAccessible(false);
                   }

               }
           }

       }



    }

}
